﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Framework;

namespace ConvertProject
{
   class StyleSymbolMap
    {
        Dictionary<string, IStyleGalleryItem> mFillSymbolMap = new Dictionary<string, IStyleGalleryItem>();
        Dictionary<string, IStyleGalleryItem> mLineSymbolMap = new Dictionary<string, IStyleGalleryItem>();
        Dictionary<string, IStyleGalleryItem> mMarkerSymbolMap = new Dictionary<string, IStyleGalleryItem>();



        IStyleGallery mStyleGallery;
        string mStylePath;

        public bool LoadStyle(string stylePath)
        {

            mStylePath = stylePath;
            mStyleGallery = new StyleGalleryClass();
            IStyleGalleryStorage styleGalleryStorage = mStyleGallery as IStyleGalleryStorage;
            styleGalleryStorage.AddFile(stylePath);
            styleGalleryStorage.TargetFile = stylePath;

            RemapFillSymbols();
            RemapLineSymbols();
            RemapMarkerSymbols();
            return true;
        }

        void RemapMarkerSymbols()
        {
            mMarkerSymbolMap.Clear();
            IStyleGalleryItem styleItem = new StyleGalleryItemClass();
            IEnumStyleGalleryItem enumItem = new EnumServerStyleGalleryItemClass();
            enumItem = mStyleGallery.get_Items("Marker Symbols", mStylePath, "");
            enumItem.Reset();
            styleItem = enumItem.Next();


            while (styleItem != null)
            {
                string name = styleItem.Name;
                if (!mMarkerSymbolMap.ContainsKey(name))
                mMarkerSymbolMap.Add(name, styleItem);

                styleItem = enumItem.Next();
            }
        }

        void RemapLineSymbols()
        {
            mLineSymbolMap.Clear();
            IStyleGalleryItem styleItem = new StyleGalleryItemClass();
            IEnumStyleGalleryItem enumItem = new EnumServerStyleGalleryItemClass();
            enumItem = mStyleGallery.get_Items("Line Symbols", mStylePath, "");
            enumItem.Reset();
            styleItem = enumItem.Next();


            while (styleItem != null)
            {
                string name = styleItem.Name;
                if (!mLineSymbolMap.ContainsKey(name))
                mLineSymbolMap.Add(name, styleItem);

                styleItem = enumItem.Next();
            }
        }

        void RemapFillSymbols()
        {
            mFillSymbolMap.Clear();
            IStyleGalleryItem styleItem = new StyleGalleryItemClass();
            IEnumStyleGalleryItem enumItem = new EnumServerStyleGalleryItemClass();

            enumItem = mStyleGallery.get_Items("Fill Symbols", mStylePath, "");
            enumItem.Reset();
            styleItem = enumItem.Next();


            while (styleItem != null)
            {
                string name = styleItem.Name;
                if (!mFillSymbolMap.ContainsKey(name))
                mFillSymbolMap.Add(name, styleItem);

                styleItem = enumItem.Next();
            }



        }

        public ISymbol GetFillSymbol(string name)
        {
            if (mFillSymbolMap.ContainsKey(name))
            {
                ISymbol srcSymbol = (ISymbol)mFillSymbolMap[name].Item;
                IClone symbolClone = (IClone)srcSymbol;
                ISymbol destSymbol = (ISymbol)symbolClone.Clone();
                return destSymbol;
            }
            return null;
        }

        public ISymbol GetLineSymbol(string name)
        {
            if (mLineSymbolMap.ContainsKey(name))
            {
                ISymbol srcSymbol = (ISymbol)mLineSymbolMap[name].Item;
                IClone symbolClone = (IClone)srcSymbol;
                ISymbol destSymbol = (ISymbol)symbolClone.Clone();
                return destSymbol;
            }
            return null;
        }

        public ISymbol GetMarkerSymbol(string name)
        {
            if (mMarkerSymbolMap.ContainsKey(name))
            {
                ISymbol srcSymbol = (ISymbol)mMarkerSymbolMap[name].Item;
                IClone symbolClone = (IClone)srcSymbol;
                ISymbol destSymbol = (ISymbol)symbolClone.Clone();
                return destSymbol;
            }
            return null;
        }
    }
}
